package com.gxa.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxa.common.JsonResult;
import com.gxa.service.CustomerService;
import com.gxa.service.impl.CustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/customer/search")
public class CustomerSearchServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取当前页码和每页显示条数
        Integer currentPage = Integer.parseInt(request.getParameter("page"));
        Integer pageSize = Integer.parseInt(request.getParameter("limit"));
//        System.out.println(currentPage+"/"+pageSize);
        String customer_name = request.getParameter("customer_name");
//        System.out.println(customer_name);
        String phone = request.getParameter("mobile_phone");
        Long mobile_phone;
        if(customer_name.isEmpty()){
            customer_name=null;
        }
        if(phone.isEmpty()) {
            mobile_phone = 0L;
        }else {
            mobile_phone = Long.parseLong(phone);
        }
//        System.out.println(mobile_phone);
        //调用service
        CustomerService customerService = new CustomerServiceImpl();
        JsonResult jsonResult = customerService.searchCustomer(currentPage, pageSize, customer_name, mobile_phone);
        //响应jsonResult
        response.setContentType("application/json; charset=UTF-8");
        response.getWriter().write(JSONObject.toJSONString(jsonResult));
    }
}
